package chances.system.enums;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import chances.system.operator.entity.EnumData;
import chances.system.operator.repo.EnumDataRepository;

@RestController
@RequestMapping(value = "/enum")
public class EnumController {

	@Autowired
	EnumDataRepository enumDataRepository;
	
	@Autowired
	EntityManagerFactory entityManagerFactory;
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/list")
	public List<EnumData> listObjValueList(HttpServletRequest httpServletRequest,
			@RequestParam(name="code") String code){
		
		List<EnumData> result = null;
		EnumData enumData = enumDataRepository.getByCode(code);
	    if(enumData == null)
            return new ArrayList<EnumData>();

		if (enumData.getType() == EnumData.TYPE_TEXT) {
			result = enumDataRepository.findByParentId(enumData.getId());
		}else if(enumData.getType()==EnumData.TYPE_HQL){
			EntityManager entityManager = entityManagerFactory.createEntityManager();
			 Query query = entityManager.createQuery(enumData.getValue());
			 result = new ArrayList<EnumData>();
			 List<Object[]> resultList = query.getResultList();
			 for (Object[] objects : resultList) {
				 EnumData eData = new EnumData();
				 eData.setValue(objects[0].toString());
				 eData.setName(objects[1].toString());
				 result.add(eData);
			 }
		}
		return result;
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping("/listSelect")
    public List<Object> listObjValueArray(HttpServletRequest httpServletRequest,
            @RequestParam(name="code") String code){
        
        List<EnumData> result = null;
        EnumData enumData = enumDataRepository.getByCode(code);
        if(enumData == null)
            return new ArrayList();
        
        if (enumData.getType() == EnumData.TYPE_TEXT) {
            result = enumDataRepository.findByParentId(enumData.getId());
        }else if(enumData.getType()==EnumData.TYPE_HQL){
            EntityManager entityManager = entityManagerFactory.createEntityManager();
             Query query = entityManager.createQuery(enumData.getValue());
             result = new ArrayList<EnumData>();
             List<Object[]> resultList = query.getResultList();
             for (Object[] objects : resultList) {
                 EnumData eData = new EnumData();
                 eData.setValue(objects[0].toString());
                 eData.setName(objects[1].toString());
                 result.add(eData);
             }
        }
        
        return result.stream().map(s -> {
            Map map = new LinkedHashMap<Object, Object>();
            map.put("code", s.getValue());
            map.put("name", s.getName());
            return map;
        }).collect(Collectors.toList());
    }
}
